<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Controller {
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	public function login_user($data)
	{
		$pword = md5($data["password"]);
		$email = $data["email"];
		$sql = "SELECT * FROM accounts_active WHERE username LIKE '$email' AND password LIKE '$pword'"; 
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0){
		   $row = $query->row(); 
		   return $row;
		}

//		redirect('', '');

	}

	public function logout_unlock($id){
		$this->load->model('admin_model');
		$q = "UPDATE student SET login_lock = 0 where md5(email_address) like '".$id."'";
		$query = $this->db->query($q);

		$query2 = "SELECT student_no from student where md5(email_address) like '".$id."'";
		$query = $this->db->query($query2);
		$res = $query->result_array();

		$this->admin_model->update_log($res[0]['student_no']." has logged out",$res[0]['student_no']);


	}

	public function login_lock_student($data){
		$this->load->model('admin_model');
		$query1 = "SELECT login_lock FROM student where md5(email_address) like '".$data."'";
		$run = $this->db->query($query1); 
		$login_lock = $run->result_array();

		if($login_lock[0]['login_lock']){
			return $login_lock[0]['login_lock'];
		}
		else{
			$q = "UPDATE student SET login_lock = 1 where md5(email_address) like '".$data."'";
		   	$query = $this->db->query($q);
			$query2 = "SELECT student_no from student where md5(email_address) like '".$data."'";
			$query = $this->db->query($query2);
			$res = $query->result_array();

			$this->admin_model->update_log($res[0]['student_no']." has logged in",$res[0]['student_no']);

			return $login_lock[0]['login_lock'];
		}
	}
/*
	Signup Info: if it exists don't insert to db
*/
	public function createStudentAccount(){
		$this->load->model('admin_model');
		$sql = "SELECT student_no from student where student_no like '".$this->input->post('stud_num')."'";

		$query = $this->db->query($sql);
		$row = $query->row();

		if(sizeof($row)) echo "User already exists";
		else{
			$sql = "SELECT email_address from student where email_address like '".$this->input->post('e_add')."'";
			$query = $this->db->query($sql);
			$row = $query->row();
			if(sizeof($row)) echo "Email already exists";
			else{
				$data = array(
						'student_no' => $this->input->post('stud_num'),
						'email_address' => $this->input->post('e_add'),
						'First_Name' => $this->input->post('f_name'),
						'Middle_Name' => $this->input->post('m_name'),
						'Last_Name' => $this->input->post('l_name'),
						'home_address' => "none",
						'home_contact' => "none",
						'college_address' => "none",
						'college_contact' => "none",
						'mobile_no' => $this->input->post('mob_num'),
						'Curriculum_taken' => "none",
						'Password' => md5($this->input->post('pword_1')),
						'Active' => "No",
						'Approved' => "TBD",
						'md5_id' => md5($this->input->post('stud_num')),
						'login_lock' => 0
					);
				$this->db->insert('student',$data);
				$log_entry = "New User: ".
							 " Student no: ".$this->input->post('stud_num').
							 " Type: Student";
				$this->admin_model->update_log($log_entry,"NEW USER");
				redirect('home','');
			}
		}

	}

	public function createFacultyAccount(){
		$this->load->model('admin_model');
		$sql = "SELECT employee_no from instructor where employee_no like '".$this->input->post('emp_num')."'";

		$query = $this->db->query($sql);
		$row = $query->row();

		if(sizeof($row)) echo "User already exists";
		else{
			$sql = "SELECT email_address from instructor where email_address like '".$this->input->post('e_add')."'";
			$query = $this->db->query($sql);
			$row = $query->row();
			if(sizeof($row)) echo "Email already exists";
			else{
				$data = array(
						'employee_no' => $this->input->post('emp_num'),
						'mobile_no' => $this->input->post('mob_num'),
						'email_address' => $this->input->post('e_add'),
						'designation_rank' => "none",
						'faculty_room' => "none",
						'sp_advisees' => "none",
						'previous_subjects_taught' => "none",
						'current_subjects' => "none",
						'Approved' => "TBD",
						'First_Name' => $this->input->post('f_name'),
						'Middle_Name' => $this->input->post('m_name'),
						'Last_Name' => $this->input->post('l_name'),
						'Password' => $this->input->post('pword_1'),
					);
				$this->db->insert('instructor',$data);
				$name = $data['Last_Name'].", ".$data['First_Name']." ".$data['Middle_Name'];

				$log_entry = "New User: ".
							 " Employee no: ".$this->input->post('emp_num').
							 " Type: Instructor/Faculty";
				$this->admin_model->update_log($log_entry,"NEW USER");
				redirect('home','');
			}
		}

	}

	public function createEncoderAccount(){

	}

	public function view_all_students()
	{
		$sql = "SELECT Student_No, First_Name, Middle_Name, Last_Name, MD5_ID FROM student WHERE Approved LIKE 'Approved'"; 
		$query = $this->db->query($sql);
		return $query;
		redirect('', '');
	}

	public function get_stdno($id)
	{
		$sql = "SELECT Student_No FROM student WHERE MD5_ID LIKE '$id'"; 
		$query = $this->db->query($sql);
		$row = $query->row(); 
		return $row->Student_No;
		redirect('', '');
	}

	public function get_approved_students(){
		$sql = "SELECT Student_No, First_Name, Middle_Name, Last_Name, MD5_ID FROM student WHERE Approved LIKE 'TBD'"; 
		$query = $this->db->query($sql);
		return $query;
		redirect('','');
	}

	public function get_approved_faculty(){
		$sql = "SELECT employee_no, First_Name, Middle_Name, Last_Name FROM instructor WHERE Approved LIKE 'TBD'"; 
		$query = $this->db->query($sql);
		return $query;
		redirect('','');
	}

	public function get_acad_info($id)
	{

		//get student's name
		$sql = "SELECT First_Name, Middle_Name, Last_Name, Curriculum_taken FROM student WHERE Student_No LIKE '$id'"; 
		$query = $this->db->query($sql);
		$row = $query->row(); 
		$std_name = $row->Last_Name.", ".$row->First_Name." ".$row->Middle_Name; 
		$std_curriculum = $row->Curriculum_taken;

		//get reg adviser's emp no
		$sql = "SELECT EMPLOYEE_NO_INSTRUCTOR FROM reg_advisee WHERE STUDENT_NO_STUDENT LIKE '$id'"; 
		$query = $this->db->query($sql);
		$row = $query->row(); 
		$instructor_empno = $row->EMPLOYEE_NO_INSTRUCTOR;


		//get reg adviser's name
		$sql = "SELECT INSTRUCTOR_NAME FROM faculty WHERE INSTRUCTOR_NUM LIKE '$instructor_empno'"; 
		$query = $this->db->query($sql);
		$row = $query->row(); 
		$reg_adviser_name = $row->INSTRUCTOR_NAME; 

		//get sp adviser's emp no
		$sql = "SELECT EMPLOYEE_NO_INSTRUCTOR FROM sp_advisee WHERE STUDENT_NO_STUDENT LIKE '$id'"; 
		$query = $this->db->query($sql);
		$row = $query->row(); 
		$instructor_empno = $row->EMPLOYEE_NO_INSTRUCTOR;

		//get sp adviser's name
		$sql = "SELECT INSTRUCTOR_NAME FROM faculty WHERE INSTRUCTOR_NUM LIKE '$instructor_empno'"; 
		$query = $this->db->query($sql);
		$row = $query->row(); 
		$sp_adviser_name = $row->INSTRUCTOR_NAME; 

		//get subject names
		$sql = "SELECT Course_Code,	Semester_Year_Taken, Grade,	Units, Subject_Type FROM subject WHERE Student_No LIKE '$id'"; 
		$query = $this->db->query($sql);
		$row = $query->row(); 
		$subjects = array(
			'course_code'  => $row->Course_Code,
			'sem_taken'	   => substr($row->Semester_Year_Taken, 0, 2),
			'yr_taken'	   => substr($row->Semester_Year_Taken, 2),
			'grade'        => $row->Grade,
			'units'		   => $row->Units,
			'type'         => $row->Subject_Type
		);

		$results = array(
			'std_name'           => $std_name,
			'std_curriculum'     => $std_curriculum,
		    'reg_adviser'        => $reg_adviser_name,
		    'sp_adviser'         => $sp_adviser_name,
		    'subjects'           => $subjects
		);

		return $results;

		redirect('', '');
	}

	
	
	
//localhost/C3L/user_guide
}

/* End of file user_model.php */
/* Location: ./application/controllers/user_model.php */